/*
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

/**
 * @namespace OAA_CI
 */

var OAA_CI = OAA_CI || {};

/**
 * @namespace OAA_CI.links
 */

OAA_CI.links = OAA_CI.links || {};

OAA_CI.links.linksView         = new OAA_CI.SidebarView('ID_TREECHILDREN_LINKS');
OAA_CI.links.duplicateHrefView = new OAA_CI.SidebarView('ID_TREECHILDREN_DUPLICATE_HREF');
OAA_CI.links.duplicateNameView = new OAA_CI.SidebarView('ID_TREECHILDREN_DUPLICATE_NAME');
OAA_CI.links.areaView          = new OAA_CI.SidebarView('ID_TREECHILDREN_AREA');

OAA_CI.links.views = [
  OAA_CI.links.linksView,
  OAA_CI.links.duplicateHrefView,
  OAA_CI.links.duplicateNameView,
  OAA_CI.links.areaView
];

OAA_CI.links.hideMeter = function () { OAA_CI.links.meter.hidden = true; };
OAA_CI.links.showMeter = function () { OAA_CI.links.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.links;

/**
 * getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 *       called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 */

OpenAjax.a11y.cache.LinkElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.name, this.getLinkType(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.DuplicateHREFItem.prototype.getDisplayProperties = function () {
  return [this.link_elements.length, this.href, ''];
};

OpenAjax.a11y.cache.DuplicateNameItem.prototype.getDisplayProperties = function () {
  return [this.link_elements.length, this.name, ''];
};

/**
 * getChildren
 *
 * @desc Add getChildren method to cache elements; called by
 *       populateTreeAsHierarchy function.
 *
 * @return List of child elements
 */

OpenAjax.a11y.cache.DuplicateHREFItem.prototype.getChildren = function () {
  return this.link_elements;
};

OpenAjax.a11y.cache.DuplicateNameItem.prototype.getChildren = function () {
  return this.link_elements;
};

/**
 * @function onLoad
 *
 * @memberOf OAA_CI.links
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.links.onLoad = function () {

  var config = {
   rules      : true,
   attributes : true,
   properties : true,
   style      : false,
   events     : true
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.links.linksView.addProperties(pt);  
  OAA_CI.links.duplicateHrefView.addProperties(pt);
  OAA_CI.links.duplicateNameView.addProperties(pt);
  OAA_CI.links.areaView.addProperties(pt);
  
  OAA_CI.links.nls_strings = document.getElementById("ID_STRINGBUNDLE_LINKS");
  OAA_CI.links.meter = document.getElementById('ID_METER_LINKS');

  OAA_CI.links.linksView.isInitialized = false;
  OAA_CI.links.linksView.showCacheItems();

};

/**
 * @function linksView.showCacheItems
 *
 * @memberOf OAA_CI.links
 *
 * @desc Respond to the All button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with link
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.links.linksView.showCacheItems = function () {
  var view = OAA_CI.links.linksView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_LINKS');
    view.tree  = document.getElementById('ID_TREE_LINKS');
    view.cache = OAA_CI.ruleset.dom_cache.links_cache;

    // Populate the tabpanel
    var link_elements = view.cache.link_elements;
    OAA_CI.populateTreeAsFlatList(link_elements, 'ID_TREECHILDREN_LINKS');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function duplicateHrefView.showCacheItems
 *
 * @memberOf OAA_CI.links
 *
 * @desc Respond to the Duplicate HREF button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with duplicate
 *       HREF items structured as a two-level list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.links.duplicateHrefView.showCacheItems = function () {
  var view = OAA_CI.links.duplicateHrefView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_DUPLICATE_HREF');
    view.tree  = document.getElementById('ID_TREE_DUPLICATE_HREF');
    view.cache = OAA_CI.ruleset.dom_cache.links_cache;

    // Populate the tabpanel
    var duplicate_href_items = view.cache.duplicate_href_items;
    OAA_CI.populateTreeAsTwoLevelList(duplicate_href_items, 'ID_TREECHILDREN_DUPLICATE_HREF');
      view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function duplicateNameView.showCacheItems
 *
 * @memberOf OAA_CI.links
 *
 * @desc Respond to the Duplicate Name button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with duplicate
 *       HREF items structured as a two-level list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.links.duplicateNameView.showCacheItems = function () {
  var view = OAA_CI.links.duplicateNameView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_DUPLICATE_NAME');
    view.tree  = document.getElementById('ID_TREE_DUPLICATE_NAME');
    view.cache = OAA_CI.ruleset.dom_cache.links_cache;

    // Populate the tabpanel
    var duplicate_name_items = view.cache.duplicate_name_items;
    OAA_CI.populateTreeAsTwoLevelList(duplicate_name_items, 'ID_TREECHILDREN_DUPLICATE_NAME');
      view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function areaView.showCacheItems
 *
 * @memberOf OAA_CI.links
 *
 * @desc Respond to the Area button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with link
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.links.areaView.showCacheItems = function () {
  var view = OAA_CI.links.areaView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_AREA');
    view.tree  = document.getElementById('ID_TREE_AREA');
    view.cache = OAA_CI.ruleset.dom_cache.links_cache;

    // Populate the tabpanel
    var area_elements = view.cache.area_elements;
    OAA_CI.populateTreeAsFlatList(area_elements, 'ID_TREECHILDREN_AREA');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.links
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the updateItemSelectionDisplay method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.links.onItemSelectionChange = function (index) {

  var view = OAA_CI.links.views[index];
  view.updateItemSelectionDisplay();

};
